/**
 * 项目名称: Topic_508.cpp
 * 项目路径: study/DataStructure/LeetCode/树/中等
 * 编辑时间: 2024年08月19日08时45分29秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<int>ans;
    unordered_map<int,int>map;
    void pre(TreeNode* root){
        if(!root)return;
        if(root->left)pre(root->left);
        if(root->right) pre(root->right);

        if(root->right)root->val+=root->right->val;
        if(root->left)root->val+=root->left->val;
        map[root->val]=map[root->val]+1;
    }
    vector<int> findFrequentTreeSum(TreeNode* root) {
        if(!root)return ans;
        int max=0;
        pre(root);
        for(auto x:map){
            if(x.second>max)max=x.second;
            cout<<x.second;
        }

        for(auto x:map){
            if(x.second == max)ans.push_back(x.first);
        }
        return ans;
    }
};
